package com.zhiche.lisa.bms.controller.contract;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.contract.Contract;
import com.zhiche.lisa.bms.dao.model.contract.ContractAccessory;
import com.zhiche.lisa.bms.dao.model.quote.Quote;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.contract.ContractAccessoryVO;
import com.zhiche.lisa.bms.pojo.vo.contract.ContractVO;
import com.zhiche.lisa.bms.pojo.vo.contract.PrintContractVO;
import com.zhiche.lisa.bms.pojo.vo.quote.QuoteCommonVO;
import com.zhiche.lisa.bms.service.contract.ContractService;
import com.zhiche.lisa.bms.service.contract.impl.ContractAccessoryServiceImpl;
import com.zhiche.lisa.bms.service.quote.QuoteService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同主表 前端控制器
 * </p>
 *
 * @author liuanshun
 * @since 2018-10-23
 */
@RestController
@RequestMapping(value = "/contract")
@Api(value = "/contract", description = "合同列表接口", tags = {"合同列表接口"})
public class ContractController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContractController.class);
    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractAccessoryServiceImpl contractAccessoryService;
    @Autowired
    private QuoteService quoteService;

    /**
     * 新增合同列表
     *
     * @param contractVO
     */
    @PostMapping(value = "/insertContractAndContractAccessories")
    @ApiOperation(value = "新增合同", notes = "新增合同", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> insertContractAndContractAccessories(@RequestBody ContractVO contractVO) {
        LOGGER.info("ContractController.insertContractAndContractAccessories param : {}", contractVO);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            contractService.insertContractAndContractAccessories(contractVO);
        } catch (BaseException e) {
            LOGGER.error("ContractController.insertContractAndContractAccessories error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ContractController.insertContractAndContractAccessories error:{}", e.getMessage());
            result.setMessage("新增合同异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 根据id获取对应的合同
     *
     * @param id
     */
    @GetMapping(value = "/getContractDetailById")
    @ApiOperation(value = "获取合同", notes = "获取合同", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = ContractVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<ContractVO> getContractDetailById(@RequestParam Long id) {
        LOGGER.info("ContractController.getContractDetailById param : {}", id);
        RestfulResponse<ContractVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(contractService.getContractDetail(id));
        } catch (BaseException e) {
            LOGGER.error("ContractController.getContractDetailById error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ContractController.getContractDetailById error:{}", e.getMessage());
            result.setMessage("获取合同");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 根据条件查询合同信息
     *
     * @param page
     * @return
     */
    @PostMapping(value = "/getContractPage")
    @ApiOperation(value = "根据条件查询合同信息", notes = "根据条件查询合同信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<Contract>> getContractPage(@RequestBody Page<Contract> page) {
        LOGGER.info("ContractController.getContractPage Param {} ", page);
        RestfulResponse<Page<Contract>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<Contract> quotePage = contractService.selectContractPage(page);
            restful.setData(quotePage);
        } catch (Exception e) {
            LOGGER.error("ContractController.getContractPage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/getContractAndQuote/{contractId}")
    @ApiOperation(value = "根据合同号获取合同和明细", notes = "根据合同号获取合同和明细", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PrintContractVO> getContractAndQuote(@PathVariable("contractId") Long contractId) {
        LOGGER.info("ContractController.getContractAndQuote Param {} ", contractId);
        RestfulResponse<PrintContractVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(contractService.selectPrintContractDetail(contractId));
        } catch (BaseException e) {
            LOGGER.error("ContractController.getContractAndQuote ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ContractController.getContractAndQuote ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/getQuoteCanBindContract/{id}")
    @ApiOperation(value = "获取报价单可绑定合同", notes = "获取报价单可绑定合同", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<Contract>> getQuoteCanBindContract(@PathVariable("id") String id) {
        LOGGER.info("ContractController.getQuoteCanBindContract Param {} ", id);
        RestfulResponse<List<Contract>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<Contract> quotePage = contractService.selectQuoteCanBindContract(id);
            restful.setData(quotePage);
        } catch (BaseException e) {
            LOGGER.error("ContractController.getQuoteCanBindContract ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ContractController.getQuoteCanBindContract ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 报价单关联合同
     *
     * @param quoteCommonVO
     * @return
     */
    @PostMapping(value = "/quoteBindContract")
    @ApiOperation(value = "报价单关联合同", notes = "报价单关联合同", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<Contract>> quoteBindContract(@RequestBody QuoteCommonVO quoteCommonVO) {
        LOGGER.info("ContractController.quoteBindContract param {} ", quoteCommonVO);
        RestfulResponse<List<Contract>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<Contract> contractList = contractService.quoteBindContract(quoteCommonVO);
            restful.setData(contractList);
        } catch (BaseException e) {
            LOGGER.error("ContractController.quoteBindContract ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ContractController.quoteBindContract ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 编辑更新合同
     *
     * @param contractVO
     */
    @PostMapping(value = "/updateContract")
    @ApiOperation(value = "编辑更新合同", notes = "编辑更新合同", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> updateContract(@RequestBody ContractVO contractVO) {
        LOGGER.info("ContractController.updateContract param : {}", contractVO);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            contractService.updateContract(contractVO);
        } catch (BaseException e) {
            LOGGER.error("ContractController.updateContract ERROR: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ContractController.updateContract ERROR: {}", e.getMessage());
            result.setMessage(CommonEnum.ERROR.getText());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 合同提交审核
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/contractRequestCheck")
    @ApiOperation(value = "合同提交审核", notes = "合同提交审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Contract> contractRequestCheck(@RequestBody List<Long> ids) {
        LOGGER.info("ContractController.contractRequestCheck param : {}", ids);
        RestfulResponse<Contract> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            contractService.contractRequestCheck(ids);
        } catch (BaseException e) {
            LOGGER.error("ContractController.contractRequestCheck ERROR: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ContractController.contractRequestCheck ERROR: {}", e.getMessage());
            result.setMessage(CommonEnum.ERROR.getText());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 合同作废
     *
     * @param ids
     */
    @PostMapping(value = "/contractInvalid")
    @ApiOperation(value = "合同提交作废", notes = "合同提交作废", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> contractInvalid(@RequestBody List<Long> ids) {
        LOGGER.info("ContractController.contractInvalid param : {}", ids);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            contractService.contractInvalid(ids);
        } catch (BaseException e) {
            LOGGER.error("ContractController.contractInvalid ERROR: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ContractController.contractInvalid ERROR: {}", e.getMessage());
            result.setMessage(CommonEnum.ERROR.getText());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 获取已有乙方名称
     */
    @GetMapping(value = "/getFleetTypeName")
    @ApiOperation(value = "获取已有乙方名称", notes = "获取已有乙方名称", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<Object>> getFleetTypeName() {
        LOGGER.info("ContractController.getFleetTypeName {}");
        RestfulResponse<List<Object>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(contractService.getFleetTypeName());
        } catch (BaseException e) {
            LOGGER.error("ContractController.getFleetTypeName ERROR: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ContractController.getFleetTypeName ERROR: {}", e.getMessage());
            result.setMessage(CommonEnum.ERROR.getText());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 根据合同主表id获取合同附件
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getContractAccessoryByHeadId")
    @ApiOperation(value = "根据合同主表id获取合同附件", notes = "根据合同主表id获取合同附件", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<ContractAccessory>> getContractAccessoryByHeadId(@RequestParam Long id) {
        LOGGER.info("ContractController.getContractAccessoryByHeadId param : {}", id);
        RestfulResponse<List<ContractAccessory>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(contractService.getContractAccessoryByHeadId(id));
        } catch (BaseException e) {
            LOGGER.error("ContractController.getContractAccessoryByHeadId ERROR: {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            LOGGER.error("ContractController.getContractAccessoryByHeadId ERROR: {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(CommonEnum.ERROR.getText());
        }
        return result;
    }

    /**
     * 根据合同主表id获取合同附件
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getContractAreExamineListByFleetId")
    @ApiOperation(value = "根据合同主表id获取合同附件", notes = "根据合同主表id获取合同附件", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<Contract>> getContractAreExamineListByFleetId(@RequestParam("id") Long id, @RequestParam(required = false, value = "queryType") String queryType) {
        LOGGER.info("ContractController.getContractAreExamineListByFleetId param : {}", id);
        RestfulResponse<List<Contract>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(contractService.getContractAreExamineListByFleetId(id, queryType));
        } catch (BaseException e) {
            LOGGER.error("ContractController.getContractAreExamineListByFleetId ERROR: {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            LOGGER.error("ContractController.getContractAreExamineListByFleetId ERROR: {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(CommonEnum.ERROR.getText());
        }
        return result;
    }

    /**
     * 下载附件
     *
     * @param id
     * @return
     */
    @GetMapping(value = "upLoadContractAccessory/{id}")
    @ApiOperation(value = "下载附件", notes = "下载附件", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<ContractAccessoryVO>> upLoadContractAccessory(@PathVariable("id") Long id) {
        LOGGER.info("ContractController.upLoadContractAccessory param : {}", id);
        RestfulResponse<List<ContractAccessoryVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(contractService.upLoadContractAccessory(id));
        } catch (BaseException e) {
            LOGGER.error("ContractController.upLoadContractAccessory ERROR: {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            LOGGER.error("ContractController.upLoadContractAccessory ERROR: {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(CommonEnum.ERROR.getText());
        }
        return result;
    }

    /**
     * 修改指定id号状态为审核
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/examine/{id}")
    @ApiOperation(value = "合同的审核确认", notes = "合同的审核确认", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Contract.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Contract> examine(@PathVariable("id") Long id) {
        LOGGER.info("ContractController.examine Param {} ", id);
        RestfulResponse<Contract> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Contract contract = contractService.examine(id);
            restful.setData(contract);
        } catch (BaseException e) {
            LOGGER.error("ContractController.examine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ContractController.examine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 修改指定id号状态为审核
     *
     * @param commonVO
     * @return
     */
    @PostMapping(value = "/examine")
    @ApiOperation(value = "合同的审核确认", notes = "合同的审核确认", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> examines(@RequestBody CommonVO<Contract> commonVO) {
        LOGGER.info("ContractController.examines Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = new ArrayList<>();
            for (Long id :
                    commonVO.getIds()) {
                try {
                    contractService.examine(id);
                    msg.add("审核::成功");
                } catch (BaseException e) {
                    msg.add("审核::" + e.getMessage());
                    restful.setCode(-2);
                } catch (Exception e) {
                    msg.add("审核::审核失败");
                    restful.setCode(-2);
                }
            }
            restful.setData(msg);
        } catch (BaseException e) {
            LOGGER.error("ContractController.examines ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ContractController.examines ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }
}

